﻿using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace blackhouse.WebNemo.Web.Controls {
    public class RecordsTable : Control {

        #region Fields

        //private NemoUrlUtils urls = NemoUrlUtils.GetUrlUtils();
        private RecordIdentyficator recordIdentyficator = new RecordIdentyficator();

        #endregion

        #region Properties

        public override bool Visible {
            get {
                return base.Visible && this.Records != null && this.Records.Count() > 0;
            }
            set {
                base.Visible = value;
            }
        }

        public bool EnableEditing {
            get;
            set;
        }

        public IEnumerable<GroupRecord> Records { get; set; }

        public RecordTableMode Mode { get; set; }

        public CultureInfo TableForTranslating { get; set; }

        #endregion

        #region Constructors

        public RecordsTable() {
            this.EnableEditing = true;
        }

        #endregion

        #region Event Handlers

        protected override void Render(HtmlTextWriter writer) {
            if (!this.Visible) return;
            this.RenderOpenTable(writer);
            foreach (GroupRecord record in this.Records) {
                this.RenderRecordRow(writer, record);
            }
            writer.RenderEndTag();
        }

        #endregion

        #region Methods

        private void RenderRecordRow(HtmlTextWriter writer, GroupRecord record) {
            this.RenderOpenRow(writer, record);
            switch (this.Mode) {
                case RecordTableMode.OneSelect:
                    this.RenderModeRadio(writer, record);
                    break;
                case RecordTableMode.MultiSelect:
                    this.RenderModeCheckbox(writer, record);
                    break;
            }
            this.RenderIdCell(writer, record);
            this.RenderIdentifierCell(writer, record);
            this.RenderTimeCell(writer, record);
            writer.RenderEndTag();
        }

        private void RenderIdCell(HtmlTextWriter writer, IGroupRecord record) {
            this.RenderOpenCell(writer, "recordid");
            writer.Write(record.RecordId.ToString());
            writer.RenderEndTag();
        }

        private void RenderTimeCell(HtmlTextWriter writer, IGroupRecord record) {
            DateTime time = record.LastModifiedTime;
            string fTime = this.FormatToMaxShortTime(time);
            this.RenderOpenCell(writer, "time");
            writer.AddAttribute(HtmlTextWriterAttribute.Title, time.ToString("F"));
            writer.AddAttribute("data-value", time.Ticks.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(fTime);
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        private string FormatToMaxShortTime(DateTime time) {
            if (time.Date == DateTime.Now.Date)
                return time.ToString("t");
            if (time.Year != DateTime.Now.Year)
                return time.ToString("d");
            return time.ToString("m");
        }

        private void RenderIdentifierCell(HtmlTextWriter writer, GroupRecord record) {
            this.RenderOpenCell(writer, "identifier");

            writer.AddAttribute(HtmlTextWriterAttribute.Title, record.Identyficator);
            writer.AddAttribute("class", "text");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(this.recordIdentyficator.GetDisplayIdentyficator(record));
            writer.RenderEndTag();

            NemoUrl urls = NemoUrl.Get();
            urls.RecordId = record.RecordId;
            if (this.TableForTranslating == null) {
                writer.AddAttribute("data-type", "open-popup"); // Ma zastosowanie do linku z podglądem
                urls.View = NemoUrlView.Preview;
                urls.AutoClose = true;
                this.RenderIdentifierCellAction(writer, urls.GetUrl(), "+ podgląd");
            }
            urls.View = NemoUrlView.EditRecord;
            urls.AutoClose = false;
            string editUrl = urls.GetUrl();
            //if (this.TableForTranslating != null)
            //    editUrl += ("&translate=" + this.TableForTranslating.Name);
            //editUrl = this.recordIdentyficator.ApplyToUrl(editUrl);
            if (this.EnableEditing)
                this.RenderIdentifierCellAction(writer, editUrl, "+ edytuj");
            if (this.EnableEditing && this.TableForTranslating == null)
                this.RenderIdentifierCellAction(writer, "remove-record:" + record.RecordId, "+ usuń");

            writer.RenderEndTag();
        }

        private void RenderIdentifierCellAction(HtmlTextWriter writer, string url, string name) {
            writer.AddAttribute("href", url);
            writer.AddAttribute("class", "action");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(name);
            writer.RenderEndTag();
        }

        private void RenderModeCheckbox(HtmlTextWriter writer, IGroupRecord record) {
            this.RenderModeSelect(writer, record, "checkbox");
        }

        private void RenderModeRadio(HtmlTextWriter writer, IGroupRecord record) {
            this.RenderModeSelect(writer, record, "radio");
        }

        private void RenderModeSelect(HtmlTextWriter writer, IGroupRecord record, string inputType) {
            this.RenderOpenCell(writer, "userSelect");
            writer.AddAttribute(HtmlTextWriterAttribute.Type, inputType);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, "record");
            writer.AddAttribute(HtmlTextWriterAttribute.Value, record.RecordId.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        private void RenderOpenCell(HtmlTextWriter writer, string cssClass) {
            string css = "cell";
            if (!String.IsNullOrEmpty(cssClass))
                css = css + " " + cssClass;
            writer.AddAttribute("class", css);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }

        private void RenderOpenRow(HtmlTextWriter writer, IGroupRecord record) {
            writer.AddAttribute("data-recordid", record.RecordId.ToString());
            writer.AddAttribute("class", "row");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }

        private void RenderOpenTable(HtmlTextWriter writer) {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "recordsTable");
            writer.AddAttribute("class", "table records" + this.Mode.ToString().ToLower());
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
        }

        #endregion

    }

    public enum RecordTableMode {
        None,
        OneSelect,
        MultiSelect
    }

}